set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

If exists(select 1 from sysobjects where xtype = 'P' and name = 'Linea_Save')
Begin
   Drop proc Linea_Save
End

GO


CREATE   Proc [dbo].[Linea_Save]
	(@Emp_Id smallint,
	 @Linea_Id int OUT,
	 @Linea_Nombre varchar(50)) AS

Begin
	Set nocount on

	If Exists(Select 1 from Linea Where Emp_Id = @Emp_Id and Linea_Id = @Linea_Id)
	Begin
		Update Linea
		Set  Linea_Nombre = @Linea_Nombre,			 
			 Fec_Actualizacion = getdate()
		Where Emp_Id = @Emp_Id
		  and Linea_Id = @Linea_Id
	End
	Else
	Begin
		Select @Linea_Id = isnull(max(Linea_Id),0)+1
        From Linea 
        Where Emp_Id = @Emp_Id

		Insert into Linea
			(Emp_Id,Linea_Id,Linea_Nombre,Fec_Actualizacion)
		Values
			(@Emp_Id,@Linea_Id,@Linea_Nombre,getdate())
	End

	Set nocount on
End





 